package ae.pcfc.cldos.online.common.strategy.transfer;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

import ae.pcfc.cldos.online.common.clddb.domain.AppMetaData;
import ae.pcfc.cldos.online.common.clddb.domain.TempFileStorage;
import ae.pcfc.cldos.online.common.messaging.GenericMessageProducer;
import ae.pcfc.cldos.online.common.vo.FileVO;
import ae.pcfc.cldos.online.common.vo.TransferDataVO;
import ae.pcfc.cldos.online.net.service.NetDataTransferService;
import ae.pcfc.cldos.online.net.service.vo.AttachmentVO;

/**
 * 
 * @author arosa
 *
 */
public abstract class BaseTransferStrategy implements ITransferToGS {

	protected NetDataTransferService netDataTransferService;
	
	public void setNetDataTransferService(
			NetDataTransferService netDataTransferService) {
		this.netDataTransferService = netDataTransferService;
	}
	
	private GenericMessageProducer genericMessageProducer;
	
	public void setGenericMessageProducer(
			GenericMessageProducer genericMessageProducer) {
		this.genericMessageProducer = genericMessageProducer;
	}
	
	protected DozerBeanMapper mapper;
	
	public void setMapper(DozerBeanMapper mapper) {
		this.mapper = mapper;
	}
	
	protected List<AttachmentVO> getAttachmentsList(AppMetaData appMetaData){
		List<TempFileStorage> fileList = appMetaData.getTempFileStorages();
		List<AttachmentVO> attachmentsList = new ArrayList<AttachmentVO>(fileList.size());
		for(TempFileStorage tempFileStorage : fileList){
			AttachmentVO attachmentVO = new AttachmentVO();
			mapper.map(tempFileStorage, attachmentVO);
			attachmentVO.setApplicationID(appMetaData.getApplicationId().toString());
			attachmentsList.add(attachmentVO);
		}
		return attachmentsList;
	}
	
	protected abstract String transferAppDateToOfflineSystem(TransferDataVO transferDataVO);
	
	public String transferToGS(TransferDataVO transferDataVO){
		List<FileVO> fileToTransfer = getApplicationFilesToTransfer(transferDataVO.getAppMetaData());
		genericMessageProducer.sendFilesToGS(fileToTransfer);
		return transferAppDateToOfflineSystem(transferDataVO);
	}
	
	private List<FileVO> getApplicationFilesToTransfer(AppMetaData appMetaData){
		List<TempFileStorage> tempFileStorages = appMetaData.getTempFileStorages();
		List<FileVO> fileVOs = new ArrayList<FileVO>(tempFileStorages.size());
		for (TempFileStorage tempFileStorage : tempFileStorages) {
			FileVO fileVO = new FileVO();
			mapper.map(tempFileStorage, fileVO);
			fileVOs.add(fileVO);
		}
		return fileVOs;
	}
}
